package stardust.product.xspider.support;


import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.ParserException;
import stardust.core.log.Log;


public class HTMLSupport
{
  
  public static final Log log = new Log(HTMLSupport.class);
  
  private static void linkFilter(List<String> result, Tag tag) throws ParserException
  {
    if (tag.getTagName().equalsIgnoreCase("a"))
    {
      if (tag.getAttribute("href") != null)
      {
        result.add(tag.getAttribute("href"));
      }
    }
    else if (tag.getTagName().equalsIgnoreCase("frame") || tag.getTagName().equalsIgnoreCase("iframe"))
    {
      if (tag.getAttribute("src") != null)
      {
        result.add(tag.getAttribute("src"));
      }
    }
    
    if (tag.getChildren() != null)
    {
      NodeIterator ni = tag.getChildren().elements();
      
      while (ni.hasMoreNodes())
      {
        Node n = ni.nextNode();
        
        if (n instanceof Tag)
        {
          linkFilter(result, (Tag) n);
        }
      }
    }
  }
    
  public static List<String> getURL(String url, String text, String encode)
  {
    final List<String> result = new ArrayList<String>();
    
    Parser parser;
    try
    {
      parser = Parser.createParser(text, encode);
      
      NodeIterator ni = parser.elements();
      while (ni.hasMoreNodes())
      {
        Node n = ni.nextNode();
        
        if (n instanceof Tag)
        {
          linkFilter(result, (Tag) n);
        }
      }
    }
    catch (ParserException e)
    {
      log.warning(e, "从[", url , "]提取URL列表时遇到错误，可能造成部分URL丢失。");
    }
    
    for (int i = 0; i < result.size(); i++)
    {
      try
      {
        result.set(i, toAbsoluteAddress(new URL(url), result.get(i)).toString());
      }
      catch (MalformedURLException e)
      {
        log.warning(e, "从[", url , "]将[", result.get(i), "]转换为绝对地址时失败。");
      }
    }

      return result;
  }
  
  public static String toAbsoluteAddress(URL referer, String uri) throws MalformedURLException
  {
    uri = uri.replaceAll("&amp;", "&");
    uri = uri.replaceAll("\\\\", "/");
    
    if (uri.toLowerCase().startsWith("http://"))
    {
      return uri.endsWith("/") ? uri.substring(0, uri.length() - 1) : uri;
    }
    
    String website = referer.getProtocol() + "://" + referer.getHost() + (referer.getPort() == -1 ? "" : ":" + referer.getPort()); // http://xxx.com:80
    
    String refererPath = referer.getPath();
    refererPath = refererPath.startsWith("/") ? refererPath.substring(1) : refererPath;
    refererPath = refererPath.endsWith("/") ? refererPath.substring(0, refererPath.length() - 1) : refererPath;
    
    uri = uri.endsWith("/") ? uri.substring(0, uri.length() - 1) : uri;
    uri = uri.indexOf('#') != -1 ? uri.substring(0, uri.indexOf('#')) : uri;
    
    String url = website + "/" + (uri.startsWith("/") ? uri.substring(1) : (refererPath.isEmpty() ? refererPath : refererPath + (uri.isEmpty() ? uri : "/" + uri)));
    
    url = url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
    
    return url;
//    URL newURL = new URL
//    (
//      referer.getProtocol() + "://" + referer.getHost() + (referer.getPort() == -1 ? "" : ":" + referer.getPort()) + "/" +
//      (
//        uri.startsWith("/")
//        ?
//        uri.substring(1)
//        :
//        (
//          (
//            referer.getPath() == null || referer.getPath().isEmpty())
//            ?
//            uri
//            :
//            (
//              (referer.getPath().endsWith("/") ? referer.getPath().substring(0, referer.getPath().length()) : referer.getPath()).substring(0, referer.getPath().lastIndexOf('/')) + "/" + uri
//            )
//        )
//      )
//    );
//    
//    return newURL.toString();
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  private static String titleFilter(Tag tag) throws ParserException
  {
    String result;
    
    if (tag.getTagName().equalsIgnoreCase("title"))
    {
      if (tag.getChildren() != null && tag.getFirstChild().getText() != null)
      {
        return tag.getFirstChild().getText();
      }
    }
    
    if (tag.getChildren() != null)
    {
      NodeIterator ni = tag.getChildren().elements();
      while (ni.hasMoreNodes())
      {
        Node n = ni.nextNode();
        
        if (n instanceof Tag)
        {
          result = titleFilter((Tag) n);
          
          if (result != null)
          {
            return result;
          }
        }
      }
    }
    
    return null;
  }
    
  public static String getTitle(String text, String encode)
  {
    Parser parser;
    String result;
    
    try
    {
      parser = Parser.createParser(text, encode);
      
      NodeIterator ni = parser.elements();
      while (ni.hasMoreNodes())
      {
        Node n = ni.nextNode();
        
        if (n instanceof Tag)
        {
          result = titleFilter((Tag) n);
          
          if (result != null)
          {
            return result;
          }
        }
      }
    }
    catch (ParserException e)
    {
      e.printStackTrace();
    }
    
    return "UNKNOW";
  }
  
}
